<?php


namespace app\api\controller\v1;

use app\api\exception\BaseException;
use app\api\validate\ExpressScene;
use app\api\validate\GoodsScene;
use app\api\validate\RefundScene;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;

/**
 * Class Express
 * @package app\api\controller\v1
 * 快递管理
 */
class Express extends BaseException
{


    /**
     * 不需要登录方法
     */
    protected $noAuth = [];

    /**
     * 获取快递列表 包含搜索功能 支持分页
     * 电话 商品名称 快递单号 联系人
     * 数据状态下拉
     * 快递公司下拉 返回初始列表的时候需要返回快递公司下拉数据
     */
    public function getExpressList(){
        $data = Request::param();
        $search = [
        ];
        if (isset($data["sort"]) && $data["sort"] == "+id") {
            $order = ["id" => "asc"];
        } else {
            $order = ["id" => "desc"];
        }
        /**
         * 单一模糊查询开始
         */
        //联系人模糊查询
        if (isset($data["contact_name"]) && !empty($data["contact_name"])) {
            $search[] = ["contact_name", "like", "%" . $data["contact_name"] . "%"];
        }
        //拆包产品名模糊查询
        if (isset($data["telephone"]) && !empty($data["telephone"])) {
            $search[] = ["telephone", "like", "%" . $data["telephone"] . "%"];
        }
        //录单姓名
        if (isset($data["goods_name"]) && !empty($data["goods_name"])) {
            $search[] = ["goods_name", "like", "%" . $data["goods_name"] . "%"];
        }
        //录单商品名称
        if (isset($data["express_num"]) && !empty($data["express_num"])) {
            $search[] = ["express_num", "like", "%" . $data["express_num"] . "%"];
        }
        /**
         * 单一模糊查询结束
         */
        /**
         *多条件查询开始
         */
        //数据状态筛选查询
        //  if (isset($data["data_state"]) && $data["data_state"] != '') {
        array_push($search, ["data_state", "=", 1]);
        //}
        //$goodsClasslist = Db::name("express")->where($search)->order($order)->page($this->page_num, $this->page_limit)->select();
        $goodsClasslist = Db::name("express")->where($search)->order($order)->page($this->page_num, $this->page_limit)->select();
        $count = Db::name("express")->where($search)->count();
        $list = [
            "total" => $count,
            "page" => $this->page_num,
            "limit" => $this->page_limit,
            "list" => $goodsClasslist
        ];
        //Db::name("account")->getLastSql();
        return self::Success($list, '获取成功', 200);
    }

    /**
     * 导入快递数据 Excel 模板提供下载
     */
    public function importExpressData(){

    }

    public function batchDelete()
    {
        $request = Request::param();
        $ids = explode(",", $request["ids"]);
        if (count($ids) != 0) {
            for ($i = 0; $i < count($ids); $i++) {
                $updateData = [
                    "data_state" => 0,
                    "update_time" => date("Y-m-d H:i:s", time()),
                ];
                Db::name("express")->where(["id" => $ids[$i]])->update($updateData);
            }
        }
        return self::Success(null, "删除成功", 200);
    }

    /**
     * 批量删除 假删除
     */
    public function batchUpdateState(){
        $request = Request::param();
        try {
            validate(RefundScene::class)
                ->scene('update_state')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            "data_state" => $request["data_state"],
            "update_time" => date("Y-m-d H:i:s", time()),
        ];
        $check = Db::name("express")->where(["id" => $request["id"]])->find();
        if ($check) {
            $result = Db::name("express")->where(["id" => $request["id"]])->update($updateData);
            if ($result) {
                return self::Success(null, "操作成功", 200);
            } else {
                return self::Error(null, '操作失败', 10002);
            }
        } else {
            return self::Error(null, '数据不存在', 10002);
        }
    }
    /**
     * 更新快递数据
     */
    public function updateExpressData(){
        $request = Request::param();
        try {
            validate(ExpressScene::class)
                ->scene('update')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $updateData = [
            "contact_name" => isset($request["contact_name"]) ? $request["contact_name"] : "",

            "telephone" => isset($request["telephone"]) ? $request["telephone"] : "",
            "goods_name" => isset($request["goods_name"]) ? $request["goods_name"] : "",
            "express_num" => isset($request["express_num"]) ? $request["express_num"]: "",
            "express_name" => isset($request["express_name"]) ? $request["express_name"]: "",
            'update_time' => date("Y-m-d H:i:s", time()),
        ];
        $result = Db::name("express")->where(["id" => $request["id"]])->update($updateData);
        if ($result) {
            return self::Success(null, "更新成功", 200);
        } else {
            return self::Error(null, '更新成功', 10002);
        }
    }
    /**
     * 添加快递数据 单条
     */
    public function addExpressData(){
        $request = Request::param();
        try {
            validate(ExpressScene::class)
                ->scene('add')
                ->check($request);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return self::Error(null, $e->getError(), 404);
        }
        $userInfo = Db::name("account")->where(["user_id" => $this->user_id])->find();

        if (isset($request["order_id"])){
            $updateOrderState = [
                'order_state' => 2,
                'update_time' => date("Y-m-d H:i:s", time()),
            ];
            Db::name("orders")->where(["id" => $request["order_id"]])->update($updateOrderState);
        }else{
            $request["order_id"] = 0;
        }
        $insertData = [
            "order_id" => $request["order_id"],
            "contact_name" => isset($request["contact_name"]) ? $request["contact_name"] : "",
            "telephone" => isset($request["telephone"]) ? $request["telephone"] : "",
            "goods_name" => isset($request["goods_name"]) ? $request["goods_name"] : "",
            "express_num" => isset($request["express_num"]) ? $request["express_num"]: "",
            "express_name" => isset($request["express_name"]) ? $request["express_name"]: "",
            'update_time' => date("Y-m-d H:i:s", time()),
            'remark' => $userInfo["real_name"],
            'status' => 1,
            'data_state' => 1,
        ];
        $result = Db::name("express")->insert($insertData);
        if ($result) {
            return self::Success(null, "添加成功", 200);
        } else {
            return self::Error(null, '添加失败', 10002);
        }
    }
}